package com.music.controller;

import com.music.dto.Result;
import com.music.entity.User;
import com.music.entity.UserLetter;
import com.music.service.UserLetterService;
import com.music.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/letter")
public class UserLetterController {
    @Autowired
    private UserLetterService userLetterService;
    @Autowired
    private UserService userService;

    @GetMapping
    public Result doGetUserLetterList(@RequestParam String userId, @RequestParam String letter_user_id){
        Result result=new Result();
        if(userId!=null && !"".equals(userId) && letter_user_id!=null && !"".equals(letter_user_id)){
            List<UserLetter> list=userLetterService.findByUserId(userId,letter_user_id);
            if(!list.isEmpty()){
                result.ok(list);
            }else{
                List<User> userList=new ArrayList<User>();
                User user=userService.findByUserId(userId);
                userList.add(user);
                User user1=userService.findByUserId(letter_user_id);
                userList.add(user1);
                result.setStatus(100);
                result.setData(userList);
            }
        }
        return result;
    }
    @GetMapping("/add")
    public Result doAdd(@RequestParam String letterContent,@RequestParam String userId, @RequestParam String letter_user_id){
        Result result=new Result();
        UserLetter userLetter=new UserLetter();
        userLetter.setUserId(userId);
        userLetter.setLetter_user_id(letter_user_id);
        userLetter.setLetterContent(letterContent);
        int a=userLetterService.add(userLetter);
        if(a>0){
            result.ok(true);
        }else {
            result.fail(0,"添加失败！");
        }
        return result;
    }
}
